package com.pig4cloud.pig.module.controller.survey;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.survey.SurveyQuestion;
import com.pig4cloud.pig.module.api.entity.survey.SurveyUserRecordInfo;
import com.pig4cloud.pig.module.service.survey.ISurveyQuestionService;
import com.pig4cloud.pig.module.service.survey.ISurveyService;
import com.pig4cloud.pig.module.service.survey.ISurveyUserRecordInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 问卷调查的用户填报数据Controller
 *
 * @author ruoyi
 * @date 2021-09-05
 */
@Validated
@Api(value = "问卷调查的用户填报数据控制器", tags = {"问卷调查的用户填报数据管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/survey/record/info")
public class SurveyUserRecordInfoController {

    private final ISurveyService surveyService;
    private final ISurveyQuestionService surveyQuestionService;
    private final ISurveyUserRecordInfoService surveyUserRecordInfoService;

    /**
     * 查询问卷调查的用户填报数据列表
     */
    @ApiOperation("查询问卷调查的用户填报数据列表")
    @PreAuthorize("@pms.hasPermission('module:survey_record:list')")
    @GetMapping("/list")
    public TableDataInfo<SurveyUserRecordInfo> list(@Validated SurveyUserRecordInfo surveyUserRecordInfo) {
        QueryWrapper<SurveyUserRecordInfo> qw = new QueryWrapper<>();
        if(surveyUserRecordInfo.getRecordId()!=null){
            qw.eq("record_id",surveyUserRecordInfo.getRecordId());
        }
        TableDataInfo<SurveyUserRecordInfo> td = PageUtils.buildDataInfo(surveyUserRecordInfoService.page(PageUtils.buildPage(),qw));
        List<SurveyUserRecordInfo> list = td.getData();
        list.forEach(record->{
            SurveyQuestion question = surveyQuestionService.getById(record.getQuestionId());
            if(question!=null){
                record.setQuestion(question.getQuestion());

                JSONArray jsonArray = JSONUtil.parseArray(question.getAnswerSection());
                JSONObject jsonObject = (JSONObject)jsonArray.get(Integer.parseInt(record.getAnswer()));
                if(jsonObject!=null && jsonObject.containsKey("value")){
                    record.setAnswerValue(jsonObject.getStr("value"));
                }
            }
        });
        return td;
    }

    /**
     * 导出问卷调查的用户填报数据列表
     */
    @ApiOperation("导出问卷调查的用户填报数据列表")
    @PreAuthorize("@pms.hasPermission('module:survey_record:export')")
    @SysLog("问卷调查的用户填报数据")
    @GetMapping("/export")
    public List export(@Validated SurveyUserRecordInfo recordInfo) {
        List<SurveyUserRecordInfo> list = surveyUserRecordInfoService.list();
        // ExcelUtil<SurveyUserRecordInfo> util = new // ExcelUtil<SurveyUserRecordInfo>(SurveyUserRecordInfo.class);
        return  list;// util.exportExcel(list, "问卷调查的用户填报数据");
    }

    /**
     * 获取问卷调查的用户填报数据详细信息
     */
    @ApiOperation("获取问卷调查的用户填报数据详细信息")
    @PreAuthorize("@pms.hasPermission('module:survey_record:query')")
    @GetMapping("/{id}")
    public R<SurveyUserRecordInfo> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
		SurveyUserRecordInfo surveyUserRecord = surveyUserRecordInfoService.getById(id);
        return R.ok(surveyUserRecord);
    }

    /**
     * 新增问卷调查的用户填报数据
     */
    @ApiOperation("新增问卷调查的用户填报数据")
    @PreAuthorize("@pms.hasPermission('module:survey_record:add')")
    @SysLog("问卷调查的用户填报数据")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody SurveyUserRecordInfo recordInfo) {
        return R.okByRow(surveyUserRecordInfoService.save(recordInfo) ? 1 : 0);
    }

    /**
     * 修改问卷调查的用户填报数据
     */
    @ApiOperation("修改问卷调查的用户填报数据")
    @PreAuthorize("@pms.hasPermission('module:survey_record:edit')")
    @SysLog("问卷调查的用户填报数据")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody SurveyUserRecordInfo recordInfo) {
        return R.okByRow(surveyUserRecordInfoService.updateById(recordInfo) ? 1 : 0);
    }

    /**
     * 删除问卷调查的用户填报数据
     */
    @ApiOperation("删除问卷调查的用户填报数据")
    @PreAuthorize("@pms.hasPermission('module:survey_record:remove')")
    @SysLog("问卷调查的用户填报数据")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(surveyUserRecordInfoService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
